//
//  CommentToolbarView.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/17.
//  Copyright © 2017年 maopu. All rights reserved.
//评论工具条

import UIKit

protocol CommentToolbarViewDelegate:class {
    func commentToolbarView(commentToolbarView:CommentToolbarView , didSelectButton button:UIButton , type:UpvoteViewType)
}

class CommentToolbarView: UIView , MoreToolViewDelegate{
    var shareBtn:UIButton!
    var commentCountBtn:UIButton!
    var upvoteBtn:UIButton!
    var downvoteBtn:UIButton!
    var amountLab:UILabel!
    
    var delegate:CommentToolbarViewDelegate!
    
    fileprivate var shareView:MoreToolView!
    
    fileprivate var likeCount:Int = 99

    override init(frame: CGRect){
        super.init(frame: frame)
        self.initializeView()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.shareBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(60)
        }
        
        self.shareBtn.snp.updateConstraints { (make) in
            make.width.equalTo(60)
        }
        
        self.commentCountBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self)
            make.left.equalTo(self.shareBtn.snp.right)
            make.bottom.equalTo(self)
            make.width.equalTo(60)
        }
        
        self.downvoteBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.right.equalTo(self)
            make.width.equalTo(30)
            make.height.equalTo(self)
        }
        
        self.amountLab.snp.makeConstraints { (make) in
            make.top.equalTo(self)
            make.right.equalTo(self.downvoteBtn.snp.left).offset(-10)
            make.bottom.equalTo(self)
            make.width.equalTo(30)
        }
        
        self.upvoteBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.right.equalTo(self.amountLab.snp.left).offset(-10)
            make.width.equalTo(30)
            make.height.equalTo(self)
        }
        self.layoutIfNeeded()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.initializeView()
    }
    
    fileprivate func initializeView() -> Void{
        self.shareBtn = UIButton.init(type: .custom)
        self.addSubview(self.shareBtn)
        self.shareBtn.tag = 100
        self.shareBtn.titleLabel?.font = UIFont.RegularDefaultFont(12)
        self.shareBtn.setTitleColor(UIColor.m_textDeepColor(), for: UIControlState.normal)
        self.shareBtn.backgroundColor = UIColor.clear
        self.shareBtn.set(image: UIImage(named: "tool_share"), title: "234", titlePosition: .right, additionalSpacing: 10, state: UIControlState())
        self.shareBtn.addTarget(self, action: #selector(self.didSelectBtnClick(button:)), for: .touchUpInside)
        
        self.commentCountBtn = UIButton.init(type: .custom)
        self.addSubview(self.commentCountBtn)
        self.commentCountBtn.titleLabel?.font = UIFont.RegularDefaultFont(12)
        self.commentCountBtn.setTitleColor(UIColor.m_textDeepColor(), for: UIControlState.normal)
        self.commentCountBtn.backgroundColor = UIColor.clear
        self.commentCountBtn.set(image: UIImage(named: "tool_comment"), title: "98", titlePosition: .right, additionalSpacing: 10, state: UIControlState())
        
        
        self.downvoteBtn = UIButton.init(type: .custom)
        self.addSubview(self.downvoteBtn)
        self.downvoteBtn.backgroundColor = UIColor.clear
        self.downvoteBtn.setImage(UIImage(named: "tool_down"), for: .normal)
        self.downvoteBtn.setImage(UIImage(named: "god_arrow_up_hl"), for: .selected)
        self.downvoteBtn.imageView?.contentMode = .scaleToFill
        self.downvoteBtn.tag = UpvoteViewType.downvote.rawValue
        self.downvoteBtn.addTarget(self, action: #selector(self.didSelectBtnClick(button:)), for: .touchUpInside)
        
        self.amountLab = UILabel()
        self.amountLab.font = UIFont.RegularDefaultFont(12)
        self.amountLab.textColor = UIColor.RGB(r: 163, g: 163, b: 163)
        self.amountLab.textAlignment = .center
        self.amountLab.contentMode = .center
        self.amountLab.text = "2K"
        self.addSubview(self.amountLab)
        
        self.upvoteBtn = UIButton.init(type: .custom)
        self.upvoteBtn.backgroundColor = UIColor.clear
        self.upvoteBtn.tag = UpvoteViewType.upvote.rawValue
        self.upvoteBtn.setImage(UIImage(named: "tool_up"), for: .normal)
        self.upvoteBtn.setImage(UIImage(named: "god_arrow_up_hl"), for: .selected)
        self.upvoteBtn.imageView?.contentMode = .scaleToFill
        self.upvoteBtn.addTarget(self, action: #selector(self.didSelectBtnClick(button:)), for: .touchUpInside)
        self.addSubview(self.upvoteBtn)
        
    }
    
    /// 刷新点赞UI数据
    ///
    /// - Parameters:
    ///   - isLike: 0：未操作   1：点赞了   2：踩了
    ///   - likeCount: 点赞踩数量
    func refreshData(isLike:Int  , likeCount:Int , shareCount:Int , commentCount:Int){
        self.likeCount = likeCount
        
        self.amountLab.text = "\(self.likeCount)"
        if isLike == 0{
            self.upvoteBtn.isSelected = false
            self.downvoteBtn.isSelected = false
        }else if isLike == 1{
            self.upvoteBtn.isSelected = true
            self.downvoteBtn.isSelected = false
        }else if isLike == 2{
            self.upvoteBtn.isSelected = false
            self.downvoteBtn.isSelected = true
        }
    }
    
    @objc fileprivate func didSelectBtnClick(button:UIButton){
        if button.tag == 100{
            if shareView == nil{
                shareView = MoreToolView.init(isVideo: true, onlyShowShare: true, onlyShowReport: false)
            }
            shareView.delegate = self
            shareView.show()
            
            return
        }else if button.tag == UpvoteViewType.downvote.rawValue{
            if self.upvoteBtn.isSelected{
                return
            }
            
        }else if button.tag == UpvoteViewType.upvote.rawValue{
            if self.downvoteBtn.isSelected{
                return
            }
        }
        if button.isSelected{
            self.likeCount -= 1
            button.isSelected = false
        }else{
            self.likeCount += 1
            button.isSelected = true
        }
        
        self.amountLab.text = "\(self.likeCount)"
        
        let type = UpvoteViewType(rawValue: button.tag)
        if self.delegate != nil{
            self.delegate.commentToolbarView(commentToolbarView: self, didSelectButton: button, type: type!)
        }
    }
    
    func moreToolView(moreToolView: MoreToolView, didSelectAt button: UIButton) {
        
    }
}
